﻿/*
 * 创建者:  ideapadqiao\qiaojianhui
 * 邮件:    qiaojianhui@cont.com.cn
 * 功能描述: 
 * 历史记录: created by qiaojianhui 2012/2/26 16:44:05  
 *           
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace oqis.gis.frame.controls
{
    public class QGUCTOC : QGUCBase
    {
        #region 类成员变量
        private System.Windows.Forms.TreeView treeViewList;

        private QGUCMapControl mMapControl;
        #endregion

        #region 类构建与初始化

        /// <summary>
        /// 类构建
        /// </summary>
        public QGUCTOC()
        {
            InitializeComponent();

            this.treeViewList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeViewList.Nodes.Clear();
            this.treeViewList.CheckBoxes = true;
        }

        /// <summary>
        /// 控件构建
        /// </summary>
        private void InitializeComponent()
        {
            this.treeViewList = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // treeViewList
            // 
            this.treeViewList.FullRowSelect = true;
            this.treeViewList.LabelEdit = true;
            this.treeViewList.Location = new System.Drawing.Point(3, 12);
            this.treeViewList.Name = "treeViewList";
            this.treeViewList.Size = new System.Drawing.Size(121, 97);
            this.treeViewList.TabIndex = 0;
            this.treeViewList.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeViewList_AfterCheck);
            // 
            // QGUCTOC
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.Controls.Add(this.treeViewList);
            this.Name = "QGUCTOC";
            this.ResumeLayout(false);

        }

        #endregion

        #region 类属性

        public QGUCMapControl BuddyControl
        {
            get { return mMapControl; }
            set
            {
                mMapControl = value;
                if (mMapControl == null) return;

                // 绑定事件监听
                mMapControl.LayerAdded += new LayerAddedHandler(mMapControl_LayerAdded);  
                mMapControl.LayerRemoved+=new LayerRemovedHandler(mMapControl_LayerRemoved);
                mMapControl.LayerCleared += delegate { this.treeViewList.Nodes.Clear(); };
            }
        }


        #endregion

        #region 类事件

        /// <summary>
        /// 图层添加事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mMapControl_LayerAdded(object sender)
        {
            try
            {
                //  判断变量是否为空
                if (mMapControl == null || mMapControl.AxMap == null) return;
                AxMapWinGIS.AxMap axMap = mMapControl.AxMap;

                // 图层句柄
                Int32 layerHandler = Convert.ToInt32(sender);

                // 获取图层名称
                string layerName = axMap.get_LayerName(layerHandler);

                // 如果图层名称为空，则从图层文件名中获取
                if (string.IsNullOrEmpty(layerName))
                {
                    string layerFile = axMap.get_LayerFilename(layerHandler);
                    layerName = System.IO.Path.GetFileNameWithoutExtension(layerFile);
                }

                // 将图层名添加至树控件
                System.Windows.Forms.TreeNode newNode = new System.Windows.Forms.TreeNode(layerName);
                newNode.Checked = true;
                this.treeViewList.Nodes.Insert(0, newNode);
                //this.treeViewList.Nodes.Add(newNode);
            }
            catch { }
        }

        
        /// <summary>
        /// 图层删除事件
        /// </summary>
        /// <param name="sender"></param>
        void mMapControl_LayerRemoved(object sender){

           
        }

        /// <summary>
        /// 选中图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeViewList_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {  //  判断变量是否为空
        	if (mMapControl == null || mMapControl.AxMap == null) return;
        	this.mAxMap = mMapControl.AxMap;

        	int layersCount = this.treeViewList.Nodes.Count;
        	//  int layerHandler = 0;
        	for (int i = 0; i < layersCount; i++)
        	{
        		// layerHandler = mAxMap.get_LayerHandle(i);

        		//mAxMap.set_LayerVisible(layerHandler, this.treeViewList.Nodes[i].Checked);
        		/*
        		 * 这种写法有待商榷
        		 */
        		mAxMap.set_LayerVisible(i, this.treeViewList.Nodes[layersCount - i - 1].Checked);
        	}
        }
        #endregion
    }
}
